package com.zhy.baselibrary.utils.dialog

import android.content.Context
import android.view.Gravity
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener

class DialogUtils private constructor() {
    private var dialogView: DialogView? = null

    companion object {
        private var dialogUtils: DialogUtils? = null
        private lateinit var mContext: Context
        fun initDialog(mContext: Context): DialogUtils {
            Companion.mContext = mContext
            if (dialogUtils == null) {
                dialogUtils = DialogUtils()
            }
            return dialogUtils!!
        }

    }

    //标题信息和显示信息
    fun builder(title: String, message: String): DialogUtils {
        dialogView = DialogView(mContext, title, message, true)
        return dialogUtils!!
    }



    //显示信息
    fun builder(message: String, gravity: Int = Gravity.CENTER): DialogUtils {
        dialogView = DialogView(mContext, "", message, false)
        return dialogUtils!!
    }

    //显示信息和右上角文案按钮
    fun builder(message: String, jump: String, jumpListener: DialogView.OnJumpListener?): DialogUtils {
        dialogView = DialogView(mContext, "", message, true)
        dialogView?.jump(jump, jumpListener)
        return dialogUtils!!
    }

    //右侧按钮颜色
    fun confirmColor(confirmColor: String): DialogUtils {
        dialogView?.confirmColor(confirmColor)
        return dialogUtils!!
    }

    //左侧按钮颜色
    fun cancelColor(cancelColor: String): DialogUtils {
        dialogView?.cancelColor(cancelColor)
        return dialogUtils!!
    }

    //左侧按钮颜色
    fun msgColor(msgColor: String): DialogUtils {
        dialogView?.msgColor(msgColor)
        return dialogUtils!!
    }

    //显示信息和右上角关闭按钮
    fun builder(message: String, isHintClose: Boolean): DialogUtils {
        dialogView = DialogView(mContext, "", message, true)
        dialogView?.close(isHintClose)
        return dialogUtils!!
    }

    //显示弹框和底部两个按钮
    fun showDialog(cancel: String = "取消", cancelListener: OnCancelListener = OnCancelListener { },
                   confirm: String = "确定", confirmListener: OnConfirmListener = OnConfirmListener { }) {
        if (dialogView == null) {
            ToastUtils.showShort("请调用builder方法")
            return
        }
        dialogView?.cancel(cancel, cancelListener)
        dialogView?.confirm(confirm, confirmListener)
        XPopup.Builder(mContext)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialogView)
                .show()
    }

    //显示弹框和底部一个按钮
    fun showDialog(confirm: String = "确定", confirmListener: OnConfirmListener = OnConfirmListener {}, isHintCancel: Boolean = true) {
        if (dialogView == null) {
            ToastUtils.showShort("请调用builder方法")
            return
        }
        dialogView?.isHintCancel(isHintCancel)
        dialogView?.confirm(confirm, confirmListener)
        XPopup.Builder(mContext)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialogView)
                .show()
    }


    //显示弹框和底部一个按钮变色
    fun showDialog(confirm: String = "确定", confirmListener: OnConfirmListener = OnConfirmListener {}, isHintCancel: Boolean = true, confirmColor: String) {
        if (dialogView == null) {
            ToastUtils.showShort("请调用builder方法")
            return
        }
        dialogView?.isHintCancel(isHintCancel)
        dialogView?.confirm(confirm, confirmColor, confirmListener)
        XPopup.Builder(mContext)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialogView)
                .show()
    }
}